﻿using DbEntityTools.Model;
using DbEntityTools.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace DbEntityTools.Config
{
    public partial class DbLogin : Form
    {
        public MySqlConnectionService service = null;
        public DbConfigModel dbModel = new DbConfigModel();

        public DbLogin()
        {
            InitializeComponent();
        }

        private void DbLogin_Load(object sender, EventArgs e)
        {
            try
            {
                this.checkBox1.Checked = true;
                InitDbConfig();
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库配置加载失败：" + ex.Message);
            }
        }

        private void InitDbConfig()
        {
            var dbNames = DbAccountService.DbConfigs.Select(x => x.DbName);
            DbNameCom.DataSource = dbNames.ToList();
        }

        private void OkBtn_Click(object sender, EventArgs e)
        {
            try
            {
                CheckDbConfig();
                service = new MySqlConnectionService(dbModel.GetConnectionStr());
                var isOk = service.TestConnection();
                if (isOk)
                {
                    SaveDbAccount();
                    this.DialogResult = DialogResult.OK;

                }
            }
            catch (Exception)
            {
                MessageBox.Show("连接数据库失败!");
            }
        }

        private void SaveDbAccount()
        {
            if (this.checkBox1.Checked)
            {
                if (DbAccountService.DbConfigs.Exists(x => x.DbName == dbModel.DbName))
                {
                    DbAccountService.DbConfigs.RemoveAll(x => x.DbName == dbModel.DbName);
                }
                DbAccountService.DbConfigs.Add(dbModel);
                DbAccountService.SaveDbAccount();
            }
        }

        private void TestConnBut_Click(object sender, EventArgs e)
        {
            try
            {
                CheckDbConfig();
                service = new MySqlConnectionService(dbModel.GetConnectionStr());
                var isOk = service.TestConnection();
                if (isOk)
                {
                    MessageBox.Show("连接数据库成功!");
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void CancaleBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private bool CheckDbConfig()
        {
            if (string.IsNullOrWhiteSpace(DbServcerText.Text))
            {
                MessageBox.Show("数据库服务器不可为空", "提示");
                DbServcerText.Focus();
                return false;
            }
            dbModel.DbServer = DbServcerText.Text.Trim();
            dbModel.DbName = DbNameCom.Text.Trim();
            if (string.IsNullOrWhiteSpace(DbNameCom.Text))
            {
                dbModel.DbName = dbModel.DbServer;
            }
            if (string.IsNullOrWhiteSpace(UidText.Text))
            {
                MessageBox.Show("用户名不可为空", "提示");
                DbServcerText.Focus();
                return false;
            }
            dbModel.UId = UidText.Text.Trim();
            if (string.IsNullOrWhiteSpace(PasswordText.Text))
            {
                MessageBox.Show("密码不可为空", "提示");
                DbServcerText.Focus();
                return false;
            }
            dbModel.Password = PasswordText.Text.Trim();
            if (string.IsNullOrWhiteSpace(DbPortText.Text))
            {
                MessageBox.Show("端口不可为空", "提示");
                DbServcerText.Focus();
                return false;
            }
            try
            {
                dbModel.Port = Convert.ToInt32(DbPortText.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("端口格式不正确", "提示");
                DbServcerText.Focus();
                return false;
            }
            return true;
        }

        private void DbNameCom_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var dbName = DbNameCom.Text.Trim();
                if (DbAccountService.DbConfigs.Exists(x => x.DbName == dbName))
                {
                    var db = DbAccountService.DbConfigs.First(x => x.DbName == dbName);
                    DbServcerText.Text = db.DbServer;
                    UidText.Text = db.UId;
                    PasswordText.Text = db.Password;
                    DbPortText.Text = db.Port.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
        }
    }
}
